package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 违纪处理
     */
    @Log
    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id, @PathVariable Integer score) {
        studentService.violation(id,score);
        return Result.success();
    }

    /**
     * 修改学生
     */
    @Log
    @PutMapping()
    public Result updateStudent(@RequestBody Student student) {
        studentService.updateStudent(student);
        return Result.success();
    }
    /**
     * 添加学生
     */
    @Log
    @PostMapping()
    public Result addStudent(@RequestBody Student student) {
        studentService.addStudent(student);
        log.info("Student:{}",student.toString());
        return Result.success();
    }


    /**
     * 批量删除学员
     */
    @Log
    @DeleteMapping("/{ids}")
    public Result deleteBatch(@PathVariable List<Integer> ids) {
        log.info("ids:{}",ids.toString());
        studentService.deleteBatch(ids);
        return Result.success();
    }



    /**
     * 根据id查询学员
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Student student = studentService.getById(id);
        return Result.success(student);
    }

    /**
     * 分页查询（带条件）
     */
    @GetMapping()
    public Result findAll(StudentQueryParam studentQueryParam) {
        log.info("studentQueryParam:{}", studentQueryParam);
        PageBean page = studentService.findAll(studentQueryParam);
        log.info("page:{}", page);
        return Result.success(page);
    }
}
